import { NextFunction, Request, Response, Router } from "express";
import { BuilderController } from "../Controller/builder_block";
import { PGApiController } from "../Controller/Gather";
import { IndexController } from "../Controller/Index";

import { isNumber } from "util";
import { Utils } from "../utils";



/**
 * / route
 *
 * @class IndexRoute
 */
export class IndexRoute  {

  /**
   * Create the routes.
   *
   * @class IndexRoute
   * @method create
   * @static
   */
  public static create(router: Router) {
    //log
    console.log("[IndexRoute::create] Creating index route.");

    //add home page route
    // router.get("/", (req: Request, res: Response, next: NextFunction) => {
    //   res.send('Hello World!');
    // });


    router.get("/", (req: Request, res: Response, next: NextFunction) => {
      console.log(process.pid);
      new IndexRoute().index(req, res, next);
    });








    // 对网站首页的访问返回 "Hello World!" 字样
    router.get('/time', (req: Request, res: Response, next: NextFunction) => {
     

      let webUrl2 = "http://www.baidu.com";//百度
      let webUrl3 = "http://www.taobao.com";//淘宝
      let webUrl4 = "http://www.ntsc.ac.cn";//中国科学院国家授时中心
      let webUrl5 = "http://www.360.cn";//360
      let webUrl6 = "http://www.beijing-time.org";//beijing-time

     // Utils.getWebsiteDatetime(webUrl2);
      Utils.getWebsiteDatetime(webUrl2).then(function(data){
         console.log(data);
      }).catch(function(error){
        console.log(webUrl2);
         console.log(error);
      });

      Utils.getWebsiteDatetime(webUrl3).then(function(data){
        console.log(data);
     }).catch(function(error){
      console.log(webUrl3);
        console.log(error);
     });

     Utils.getWebsiteDatetime(webUrl4).then(function(data){
      console.log(data);
   }).catch(function(error){
    console.log(webUrl4);
      console.log(error);
   });

   Utils.getWebsiteDatetime(webUrl5).then(function(data){
    console.log(data);
 }).catch(function(error){
  console.log(webUrl5);
    console.log(error);
 });

 Utils.getWebsiteDatetime(webUrl6).then(function(data){
  console.log(data);
}).catch(function(error){
  console.log(webUrl6);
  console.log(error);
});
 

      
      // console.log(  Utils.getWebsiteDatetime(webUrl4));
      // console.log(  Utils.getWebsiteDatetime(webUrl5));
      // console.log(  Utils.getWebsiteDatetime(webUrl6));
      res.send('Hello World!');
    });


    router.get('/block', (req: Request, res: Response, next: NextFunction) => {
      let control = new BuilderController();
      control.createBlock(req, res, next);
    }, (req: Request, res: Response, next: NextFunction) => {
        let control = new BuilderController();
        control.getLastBlock(req, res, next);
    });

    router.get('/async/block', (req: Request, res: Response, next: NextFunction) => {
      let data: any = new BuilderController().asyncCreateBlock();
      console.log('go block');
      res.json(data);
    });




    // 对网站首页的访问返回 "Hello World!" 字样
    router.get('/demo', (req: Request, res: Response, next: NextFunction) => {

      // var re =/^[1-9]+[0-9]*]*$/; //判断字符串是否为数字 //判断正整数  

      // if (!re.test('12345')) { 
      //     res.send(1); 
      //   } else if (!re.test('bdfds')) {
      //     res.send(2);
      //   } 
      // res.send(true);
      console.log('demo');
      res.send('Got a GET request');
    });

    // 网站首页接受 POST 请求
    router.post('/', (req: Request, res: Response, next: NextFunction) => {
      res.send('Got a POST request');
    });

    // /user 节点接受 PUT 请求
    router.put('/user', (req: Request, res: Response, next: NextFunction) => {
      res.send('Got a PUT request at /user');
    });

    // /user 节点接受 DELETE 请求
    router.delete('/user', (req: Request, res: Response, next: NextFunction) => {
      res.send('Got a DELETE request at /user');
    });

    //all 接收所有请求
    router.all('/select', (req: Request, res: Response, next: NextFunction) => {
      console.log('Accessing the secret section ...1');
      next(); // pass control to the next handler 
    }, (req: Request, res: Response, next: NextFunction) => {
      console.log('Accessing the secret section ...2');
      next(); // pass control to the next handler 
    }, (req: Request, res: Response, next: NextFunction) => {
      console.log('Accessing the secret section ...3');
      res.send('Got a all request at /select');
    });




    // 对网站首页的访问返回 "Hello World!" 字样
    router.all('/gather/:type/:url', (req: Request, res: Response, next: NextFunction) => {  
      next(); 
    }, (req: Request, res: Response, next: NextFunction) => { 

      let control = new PGApiController();
      control.addService(req, res, next);

       
    });


      
      router.all('/gather/start', (req: Request, res: Response, next: NextFunction) => {  
        next(); 
      }, (req: Request, res: Response, next: NextFunction) => { 
  
        let control = new PGApiController();
        control.startService(req, res, next);
  
         
      });

      router.all('/gather/stop', (req: Request, res: Response, next: NextFunction) => {  
        next(); 
      }, (req: Request, res: Response, next: NextFunction) => { 
  
        let control = new PGApiController();
        control.stopService(req, res, next);
  
         
      });

  }



  /**
   * The home page route.
   *
   * @class IndexRoute
   * @method index
   * @param req {Request} The express Request object.
   * @param res {Response} The express Response object.
   * @next {NextFunction} Execute the next method.
   */
  public index(req: Request, res: Response, next: NextFunction) {
 
    //set message
    let options: Object = {
      "message": "Welcome to the Tour of Heros",
      "title":"Home | Tour of Heros"
    };

    //render template

    res.render("index", options); 
  }

}
